using System;
using System.Threading;

using Microsoft.SPOT;

using GHIElectronics.NETMF.USBHost;

namespace FEZ_Cobra_Console_Application1
{
    class TelitDriver
    {
        USBH_RawDevice controller;

        USBH_Device modemModule;
        USBH_SerialUSB modemSerial;
        USBH_Device nmeaReadings; //GPS, in NMEA Format
        USBH_SerialUSB nmeaSerial;
        USBH_Device diagnosticInterface;
        USBH_SerialUSB diagnosticSerial;
        USBH_Device auxilliaryPort;
        USBH_SerialUSB auxilliarySerial;

        int connectAttempts = 1;

        public TelitDriver()
        {
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
        }

        void DeviceConnectedEvent(USBH_Device device)
        {
            Debug.Print("[Device, Port " + device.PORT_NUMBER + "], ID: " + device.ID);
            controller = new USBH_RawDevice(device);
            //Debug.Print("Type: " + DeviceTypeToString(device.TYPE));
            Debug.Print("ProductID: " + controller.PRODUCT_ID);
            Debug.Print("VendorID: " + controller.VENDOR_ID);

            //if (connectAttempts == 4)
            {
                //dont need to verify types of anything because I am only using this device for now
                try
                {
                    if (auxilliaryPort == null)
                    {
                        auxilliaryPort = new USBH_Device(device.ID, device.INTERFACE_INDEX, device.TYPE, device.VENDOR_ID, device.PRODUCT_ID, device.PORT_NUMBER);
                        auxilliarySerial = new USBH_SerialUSB(auxilliaryPort, 115200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                    }
                    /*
                    if (auxilliaryPort == null)
                    {
                        auxilliaryPort = new USBH_Device(device.ID, 3, device.TYPE, device.VENDOR_ID, device.PRODUCT_ID, device.PORT_NUMBER);
                        auxilliarySerial = new USBH_SerialUSB(auxilliaryPort, 115200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                    }

                    if (diagnosticInterface == null)
                    {
                        diagnosticInterface = new USBH_Device(device.ID, 1, device.TYPE, device.VENDOR_ID, device.PRODUCT_ID, device.PORT_NUMBER);
                        diagnosticSerial = new USBH_SerialUSB(diagnosticInterface, 115200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                    }

                    if (nmeaReadings == null)
                    {
                        nmeaReadings = new USBH_Device(device.ID, 2, device.TYPE, device.VENDOR_ID, device.PRODUCT_ID, device.PORT_NUMBER);
                        nmeaSerial = new USBH_SerialUSB(nmeaReadings, 115200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                    }

                    if (modemModule == null)
                    {
                        modemModule = new USBH_Device(device.ID, 0, device.TYPE, device.VENDOR_ID, device.PRODUCT_ID, device.PORT_NUMBER);
                        modemSerial = new USBH_SerialUSB(modemModule, 115200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                    }
                    */

                    //Cannot get past this point to go ahead and initialize the modem, as in the PPP example online
                }
                catch (Exception ea)
                {
                    Debug.Print("Problem initializing USB Modem: " + ea.Message);
                }
            }

            connectAttempts++;
        }
    }
}